package view
{
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.ui.Keyboard;
	
	import flashx.textLayout.formats.LineBreak;
	
	import model.data.Book;
	
	import spark.components.Button;
	import spark.components.supportClasses.StyleableTextField;
	
	import utils.ParagraphProcessor;

	public class ParagraphView extends BaseView
	{
		private var __book:Book;
		private var __processor:ParagraphProcessor;
		
		private var __actualParagraph:int;
		private var __actualParagraphIndex:int;
		
		public function ParagraphView()
		{
			super();
		}
		
		private var labelField:StyleableTextField;
		private var buttonLeft:Button;
		private var buttonRight:Button;
		
		override protected function commitProperties():void
		{
			super.commitProperties();
		}
		
		override protected function createChildren():void
		{
			super.createChildren();
			
			addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
			
			buttonLeft = new Button;
			buttonLeft.setActualSize(30,30);
			buttonLeft.addEventListener(MouseEvent.CLICK, onLeft);
			addChild(buttonLeft);
			
			buttonRight = new Button;
			buttonRight.setActualSize(30,30);
			buttonRight.addEventListener(MouseEvent.CLICK, onRight);
			addChild(buttonRight);
			
			labelField = new StyleableTextField;					
			labelField.lineBreak = LineBreak.TO_FIT;		
			labelField.enabled = false;
			addChild(labelField);		
			
			labelField.styleDeclaration = styleManager.getStyleDeclaration(".paragraphStyle");
			labelField.commitStyles();
			
			__processor = new ParagraphProcessor;
		}
		
		protected function onKeyDown(event:KeyboardEvent):void
		{
			if (event.keyCode == Keyboard.LEFT)
				onLeft();
					
			if (event.keyCode == Keyboard.RIGHT)
				onRight();
		}
		
		protected function onLeft(event:MouseEvent=null):void
		{
			trace("Left");
			changeParagraph(-1);
		}
		
		protected function onRight(event:MouseEvent=null):void
		{
			trace("Right");
			changeParagraph(1);
		}
		
		private function changeParagraph(value:int):void
		{
			__actualParagraphIndex = __actualParagraphIndex + value;
			
			if (__actualParagraphIndex < 0 || __actualParagraphIndex > __book.paragraphList.length-1)
				__actualParagraphIndex = __actualParagraphIndex - value;
			
			__actualParagraph = __book.paragraphList[__actualParagraphIndex];
			
			setParagraph(__actualParagraph);
		}
		
		override protected function updateDisplayList(w:Number, h:Number):void
		{
			super.updateDisplayList(w, h);
			
			buttonLeft.x = (w - buttonLeft.width - buttonRight.width - 10)/2;
			buttonLeft.y = 10;
			
			buttonRight.x = buttonLeft.x + buttonLeft.width + 10;
			buttonRight.y = buttonLeft.y;
			
			labelField.y = buttonLeft.y + buttonLeft.height + 10;
			
			labelField.width = w;
			labelField.height  = h - labelField.y;
		}

		public function setParagraph(value:int):void
		{
			__actualParagraph = value;
			__actualParagraphIndex = __book.getParagraphIndex(value);
		
			trace(__actualParagraph, __actualParagraphIndex);
			labelField.htmlText = __processor.processParagraph(__book.getParagraph(__actualParagraph));
		}
		
		public function setBook(b:Book):void
		{
			__book = b;			
		}
	}
}